package com.fjwt.gz.service.activity.platform.acte017;

import cn.hutool.core.util.ObjectUtil;
import com.alibaba.fastjson2.JSONObject;
import com.fjwt.gz.core.constants.Constants;
import com.fjwt.gz.core.exception.BizException;
import com.fjwt.gz.db.entity.SysExportTaskEntity;
import com.fjwt.gz.db.vo.Acte017ExportVO;
import com.fjwt.gz.service.activity.AbstractExportReport;
import com.fjwt.gz.service.impl.FansAwardService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

@Slf4j
@Service
public class ACTE017ExportReport extends AbstractExportReport<Acte017ExportVO> {

    @Autowired
    private FansAwardService fansAwardService;

    private static final Map<String, String> headerAliasMapOfCoupon = new LinkedHashMap<>();
    private static final Map<String, String> headerAliasMapOfGoods = new LinkedHashMap<>();
    private static final Map<String, String> headerAliasMapOfScore = new LinkedHashMap<>();

    static {
        // 卡券
        headerAliasMapOfCoupon.put("fansId", "用户唯一ID");
        headerAliasMapOfCoupon.put("realName", "参与客户姓名");
        headerAliasMapOfCoupon.put("phone", "参与客户手机号");
        headerAliasMapOfCoupon.put("isAuth", "参与客户是否实名");
        headerAliasMapOfCoupon.put("couponName", "领用卡券名称");
        headerAliasMapOfCoupon.put("couponPrice", "卡券金额");
        headerAliasMapOfCoupon.put("thirdCouponId", "微信唯一卡券编号");
        headerAliasMapOfCoupon.put("payConfig", "垫资方唯一卡券编号"); //
        headerAliasMapOfCoupon.put("createdAt", "参与时间");
        headerAliasMapOfCoupon.put("writeTime", "核销时间");
        headerAliasMapOfCoupon.put("state", "卡券状态");
        headerAliasMapOfCoupon.put("channelType", "入链渠道"); //
        headerAliasMapOfCoupon.put("channelCode", "入链账号"); //
        headerAliasMapOfCoupon.put("channelName", "入链账号名称"); //
        headerAliasMapOfCoupon.put("channelAgentNo", "入链账号所属网点机构号");
        headerAliasMapOfCoupon.put("fansAgentNo", "激活客户经理归属机构号");
        headerAliasMapOfCoupon.put("fansAgentName", "激活客户经理归属网点");
        headerAliasMapOfCoupon.put("fansRealName", "激活客户经理姓名");
        headerAliasMapOfCoupon.put("fansRealPhone", "激活客户经理手机号");

        // 实物
        headerAliasMapOfGoods.put("fansId", "用户唯一ID");
        headerAliasMapOfGoods.put("realName", "参与客户姓名");
        headerAliasMapOfGoods.put("phone", "参与客户手机号");
        headerAliasMapOfGoods.put("isAuth", "参与客户是否实名");
        headerAliasMapOfGoods.put("couponName", "领用实物名称");
        headerAliasMapOfGoods.put("createdAt", "参与时间");
        headerAliasMapOfGoods.put("writeTime", "核销时间");
        headerAliasMapOfGoods.put("state", "卡券状态");
        headerAliasMapOfGoods.put("channelType", "入链渠道"); //
        headerAliasMapOfGoods.put("channelCode", "入链账号"); //
        headerAliasMapOfGoods.put("channelName", "入链账号名称"); //
        headerAliasMapOfGoods.put("channelAgentNo", "入链账号所属网点机构号");
        headerAliasMapOfGoods.put("fansAgentNo", "激活客户经理归属机构号");
        headerAliasMapOfGoods.put("fansAgentName", "激活客户经理归属网点");
        headerAliasMapOfGoods.put("fansRealName", "激活客户经理姓名");
        headerAliasMapOfGoods.put("fansRealPhone", "激活客户经理手机号");

        // 积分
        headerAliasMapOfScore.put("fansId", "用户唯一ID");
        headerAliasMapOfScore.put("realName", "参与客户姓名");
        headerAliasMapOfScore.put("phone", "参与客户手机号");
        headerAliasMapOfScore.put("isAuth", "参与客户是否实名");
        headerAliasMapOfScore.put("name", "福分名称");
        headerAliasMapOfScore.put("score", "福分值");
        headerAliasMapOfScore.put("createdAt", "参与时间");
        headerAliasMapOfScore.put("channelType", "入链渠道"); //
        headerAliasMapOfScore.put("channelCode", "入链账号"); //
        headerAliasMapOfScore.put("channelName", "入链账号名称"); //
        headerAliasMapOfScore.put("channelAgentNo", "入链账号所属网点机构号");
    }

    @Override
    public String exportActivityExcel(SysExportTaskEntity sysExportTaskEntity) {
        // 获取传参 并转换为JsonObject
        JSONObject reqParam = getRequestParam(sysExportTaskEntity.getRequestParam());
        // 分奖品类型进行导出
        switch (reqParam.getIntValue("awardType")) {
            case Constants.AWARD.COUPON:
                List<Acte017ExportVO> couponAwardList = fansAwardService.exportACTE017CouponAward(reqParam.getString("awardType"), reqParam.getString("awardId"));
                if (ObjectUtil.isEmpty(couponAwardList)){
                    throw new BizException("该活动暂无奖品数据！");
                }
                return createExcelAndUploadOssUrl(
                        couponAwardList,
                        headerAliasMapOfCoupon,
                        sysExportTaskEntity);
            case Constants.AWARD.GOODS:
                List<Acte017ExportVO> goodsAwardList = fansAwardService.exportACTE017GoodsAward(reqParam.getString("awardType"), reqParam.getString("awardId"));
                if (ObjectUtil.isEmpty(goodsAwardList)){
                    throw new BizException("该活动暂无奖品数据！");
                }
                return createExcelAndUploadOssUrl(
                        goodsAwardList,
                        headerAliasMapOfGoods,
                        sysExportTaskEntity);
            case Constants.AWARD.SCORE:
                List<Acte017ExportVO> scoreAwardList = fansAwardService.exportACTE017ScoreAward(reqParam.getString("activityId"));
                if (ObjectUtil.isEmpty(scoreAwardList)){
                    throw new BizException("该活动暂无奖品数据！");
                }
                return createExcelAndUploadOssUrl(
                        scoreAwardList,
                        headerAliasMapOfScore,
                        sysExportTaskEntity);
        }
        return "";
    }

}
